package top.cdmrx.www.demo.control;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import top.cdmrx.common.R;
import top.cdmrx.www.demo.service.VideoVodService;
import top.cdmrx.www.demo.utils.AliyunVodSDKUtils;

import java.util.List;

/**
 * Created by Mr.陈 time on 2021/11/8 | 21:16
 **/
@RestController
@RequestMapping("/eduvod/video")
@CrossOrigin
public class VodController {

    @Autowired
    private VideoVodService videoVodService;

    /**
     * 上传视频到阿里云
     * @param file
     * @return
     */
    @PostMapping("uploadAliyunVideo")
    public R uploadAliyunVideo(@ApiParam(name = "file",value = "文件",required = true)
            @RequestParam("file") MultipartFile file){
        //返回上传视频id
        String videoId = videoVodService.uploadVideoAly(file);
        return R.success().data("videoId",videoId);
    }

    /**
     * 根据视频id删除阿里云视频
     * @param id
     * @return
     */
    @DeleteMapping("removeAlyVideo/{id}")
    public R removeAlyVideo(@PathVariable String id){
        try {
            //初始化对象
            DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(AliyunVodSDKUtils.ACCESS_KEY_ID, AliyunVodSDKUtils.ACCESS_KEY_SECRET);
            //删除视频的request对象
            DeleteVideoRequest request = new DeleteVideoRequest();
            //向request中设置视频的id
            request.setVideoIds(id);
            //调用初始化对象的方法来实现删除
            client.getAcsResponse(request);
            return R.success();

        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("删除失败");
        }
    }

    @DeleteMapping("delete-batch")
    public R deleteBatch(@RequestParam("videoIdList")List<String> videoIdList){

        videoVodService.removeMoreAlyVideo(videoIdList);

        return R.success();
    }
}
